package com.hmily.spring.samples.trace;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <h1>追踪日志ID验证的控制器。</h1>
 *
 * @author hmilyylimh
 * ^_^
 * @version 0.0.1
 * ^_^
 * @date 2021-10-31
 */
@RestController
public class TraceController {

    private static final Logger logger = LoggerFactory.getLogger(TraceController.class);

    @RequestMapping("/trace")
    public String getTrace(){
        logger.info("recv trace controller request.");

        String traceId = MDC.get(TraceConsts.TRACE_ID);
        String spanId = MDC.get(TraceConsts.SPAN_ID);

        return "traceId=" + traceId + ", spanId="+ spanId;
    }

    public void demo(){
//        submit(wrapper(submit(Callable<T> task)))
//
//        Map<String, String> copyOfContextMap = MDC.getCopyOfContextMap();
//
//        run方法
//        MDC.setContextMap(copyOfContextMap);



//        ThreadPoolExecutor executor;
//        Map<String, String> copyOfContextMap = MDC.getCopyOfContextMap();
//        executor.submit(new Runnable() {
//            @Override
//            public void run() {
//                MDC.setContextMap(copyOfContextMap);
//                MDC.put(TraceConsts.SPAN_ID, UUID.randomUUID().toString().replace("-", ""));
//            }
//        });
    }

//    public <T> Future<T> submit(Callable<T> task) {
//        if (task == null) throw new NullPointerException();
//        RunnableFuture<T> ftask = newTaskFor(task);
//        execute(ftask);
//        return ftask;
//    }
}